package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.CoinTransfer;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;
import com.srwl.mytx.utils.DateUtils;

import java.util.List;


public class TransferRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<CoinTransfer> coinTransferList;
    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否有数据


    public TransferRecordAdapter(Context context, List<CoinTransfer> coinTransferList) {
        mContext = context;
        this.coinTransferList = coinTransferList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       /* if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(mContext).inflate(R.layout.no__data__layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transfer_record_list_item, parent, false);
        return new ItemViewHolder(view);*/

        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(mContext).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transfer_record_list_item, parent, false);
        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            ((NoDataViewHolder) viewHolder).tv_tips.setText("还没有转账记录");
            return;
        }

        if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return;
        }
        ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
        CoinTransfer coinTransfer = coinTransferList.get(position);
        User sendUser = coinTransfer.getTargetUser();
        itemViewHolder.tvDate.setText(DateUtils.dateToString(coinTransfer.getCreated(), DateUtils.FORMAT_TIMESTAMP));
        if (sendUser != null) {
            itemViewHolder.tvSenderName.setText(sendUser.getUsername());
//        Glide.with(mContext).load(sendUser.getAvatar())
//                .placeholder(R.drawable.rp_avatar)
//                .into(itemViewHolder.ivAvatar);
            GlideLoadUtils.glideLoad(mContext, sendUser.getAvatar(), itemViewHolder.ivAvatar, R.drawable.tx_default_avatar_1);
        }
        String showMoney = "";
        if (coinTransfer.getuId().equals(UserProfileManager.getInstance().getLoginUserInfo().getuId())) {
            showMoney = "- " + Utils.moneyFormat(coinTransfer.getAmount());
            itemViewHolder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.text_black));

        } else {
            showMoney = "+ " + Utils.moneyFormat(coinTransfer.getAmount());
            itemViewHolder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.rp_money_red_light));
        }
        itemViewHolder.tvMoney.setText(showMoney);
    }


    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return coinTransferList.size();

    }


    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvSenderName;
        TextView tvMoney;
        TextView tvDate;
        ImageView ivAvatar;
        TextView tvType;

        View vLineNoHead;

        ItemViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            tvSenderName = v.findViewById(R.id.tv_sender_nickname);
            tvMoney = v.findViewById(R.id.tv_item_money_amount);
            tvDate = v.findViewById(R.id.tv_time);
            ivAvatar = v.findViewById(R.id.iv_record_sender_avatar);
            tvType = v.findViewById(R.id.tv_item_type);
            vLineNoHead = v.findViewById(R.id.view_line_no_head);
        }

    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }

}
